package com.pig4cloud.pig.village.appcollect.service.impl;

import cn.hutool.core.date.DateUtil;
import com.pig4cloud.pig.village.api.app.*;
import com.pig4cloud.pig.village.api.dto.LandDTO;
import com.pig4cloud.pig.village.api.dto.LandTotalDTO;
import com.pig4cloud.pig.village.api.entity.Fund;
import com.pig4cloud.pig.village.api.entity.Message;
import com.pig4cloud.pig.village.api.entity.RewardInfo;
import com.pig4cloud.pig.village.api.entity.Welfare;
import com.pig4cloud.pig.village.appcollect.service.IAppCollectService;
import com.pig4cloud.pig.village.fund.service.IFundService;
import com.pig4cloud.pig.village.land.service.ILandService;
import com.pig4cloud.pig.village.message.service.IMessageService;
import com.pig4cloud.pig.village.reward.service.IRewardService;
import com.pig4cloud.pig.village.villager.service.IVillagerService;
import com.pig4cloud.pig.village.welfare.service.IWelfareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author limbray.li
 * @date 2021/7/3
 */
@Service
public class AppCollectServiceImpl implements IAppCollectService {
	@Resource
	private IRewardService rewardService;
	@Resource
	private ILandService landService;
	@Resource
	private IVillagerService villagerService;
	@Resource
	private IMessageService messageService;
	@Resource
	private IFundService fundService;
	@Resource
	private IWelfareService welfareService;

	@Override
	public AppMainDto getMainPageData() {
		AppMainDto dto = new AppMainDto();
		//本月新增违规事件
		dto.getLeft().add(monthBreak());
		//本月新增表彰事件
		dto.getLeft().add(monthHonour());
		//今年违规事件
		dto.getLeft().add(yearBreak());
		//今年表彰事件
		dto.getLeft().add(yearHonour());
		//常住总数，非常住居民，低收入人数，体检普及人数，土地面积（亩）
		dto.getRight().add(resideTotal());
		dto.getRight().add(nonResidentTotal());
		dto.getRight().add(lowIncome());
		dto.getRight().add(examination());
		dto.getRight().add(land());
		//获取滚动消息
		dto.getMsgTop().addAll(getMessageTop());
		dto.getMsgBottom().addAll(getMessageBottom());
		return dto;
	}

	@Override
	public AppCollectDto getFundPageData() {
		AppCollectDto appCollectDto = new AppCollectDto();
		//基本信息
		appCollectDto.getTop().add(yearBreak());
		appCollectDto.getTop().add(yearHonour());
		appCollectDto.getLeft().addAll(fundList());
		appCollectDto.getRight().addAll(autonomousList());
		appCollectDto.getBottom().addAll(rewardList(2021, null));
		return appCollectDto;
	}

	@Override
	public AppEventDto getEventPageData(Integer year, Integer punishment) {
		AppEventDto eventDto = new AppEventDto();
		eventDto.getTop().addAll(rewardList(year, punishment));
		eventDto.getBottom().addAll(eventDetailList(year, punishment));
		return eventDto;
	}

	private MainPageItem monthBreak() {
		int count = rewardService.countByMonth(DateUtil.thisYear(), DateUtil.thisMonth(), 1);
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("monthAddBreak")
				.name("本月新增违规事件")
				.count(value)
				.build();
	}

	private MainPageItem monthHonour() {
		int count = rewardService.countByMonth(DateUtil.thisYear(), DateUtil.thisMonth(), 0);
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("monthAddHonour")
				.name("本月新增表彰事件")
				.count(value)
				.build();
	}

	private MainPageItem yearBreak() {
		int count = rewardService.countByYear(DateUtil.thisYear(), 1);
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("yearBreak")
				.name("今年违规事件")
				.count(value)
				.build();
	}

	private MainPageItem yearHonour() {
		int count = rewardService.countByYear(DateUtil.thisYear(), 0);
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("yearHonour")
				.name("今年表彰事件")
				.count(value)
				.build();
	}

	private MainPageItem resideTotal() {
		int count = villagerService.resideTotal();
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("resideTotal")
				.name("常住总数")
				.count(value)
				.build();
	}

	private MainPageItem nonResidentTotal() {
		int count = villagerService.nonResidentTotal();
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("lodgdTotal")
				.name("非常住居民")
				.count(value)
				.build();
	}

	private MainPageItem lowIncome() {
		int count = villagerService.lowIncomeTotal();
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("lowIncome")
				.name("低收入人数")
				.count(value)
				.build();
	}

	private MainPageItem examination() {
		int count = 1200;
		String value = String.valueOf(count);
		return MainPageItem.builder()
				.code("examination")
				.name("体检普及人数")
				.count(value)
				.build();
	}

	private MainPageItem land() {
		LandTotalDTO dto = landService.villageTotal();
		String value = String.valueOf(dto.getTotal());
		return MainPageItem.builder()
				.code("land")
				.name("土地面积（亩）")
				.count(value)
				.build();
	}

	private List<MainPageMessage> getMessageTop() {
		LocalDateTime dateTime = DateUtil.toLocalDateTime(DateUtil.date());
		List<Message> messages = messageService.getMessageAfter(dateTime);
		List<MainPageMessage> list = messages.stream()
				.filter(e -> e.getType() == 0)
				.map(e -> MainPageMessage.builder()
						.title(e.getTitle())
						.date(e.getDate().toLocalDate())
						.context(e.getContent())
						.build()).collect(Collectors.toList());
		return list;
	}

	private List<MainPageMessage> getMessageBottom() {
		LocalDateTime dateTime = DateUtil.toLocalDateTime(DateUtil.date());
		List<Message> messages = messageService.getMessageAfter(dateTime);
		List<MainPageMessage> list = messages.stream()
				.filter(e -> e.getType() == 1)
				.map(e -> MainPageMessage.builder()
						.title(e.getTitle())
						.date(e.getDate().toLocalDate())
						.context(e.getContent())
						.build()).collect(Collectors.toList());
		return list;
	}
/*

	private MainPageItem autonomousTotal() {

		String value = String.valueOf(100000);
		return MainPageItem.builder()
				.code("autonomousTotal")
				.name("自治金总额")
				.count(value)
				.build();
	}

	private MainPageItem autonomousRest() {
		String value = String.valueOf(5000);
		return MainPageItem.builder()
				.code("autonomousRest")
				.name("自治金剩余")
				.count(value)
				.build();
	}
*/

	private List<MainPageItem> fundList() {
		String value = String.valueOf(1000);
		List<MainPageItem> list = new ArrayList<>();
		list.add(MainPageItem.builder()
				.code("grant")
				.name("助学金")
				.count(value)
				.build());
		list.add(MainPageItem.builder()
				.code("custom")
				.name("风尚金")
				.count(value)
				.build());
		list.add(MainPageItem.builder()
				.code("assistance")
				.name("帮扶金")
				.count(value)
				.build());
		list.add(MainPageItem.builder()
				.code("share")
				.name("共享金")
				.count(value)
				.build());
		list.add(MainPageItem.builder()
				.code("autonomous")
				.name("自治金")
				.count(value)
				.build());
		return list;
	}

	private List<MainPageItem> autonomousList() {
		String value = String.valueOf(1000);
		List<MainPageItem> list = new ArrayList<>();
		list.add(MainPageItem.builder()
				.code("deduction")
				.name("扣除金额")
				.count(value)
				.build());
		list.add(MainPageItem.builder()
				.code("rest")
				.name("剩余金额")
				.count(value)
				.build());
		return list;
	}

	private List<MainReward> rewardList(Integer year, Integer punishment) {
		List<MainReward> list = rewardService.mainRewardList(year, punishment);
		return list;
	}

	private List<MainPageItem> eventDetailList(Integer year, Integer punishment) {
		List<MainPageItem> result = new ArrayList<>();
		String fundCode = "autonomousFund";
		//获取自治金id
		Fund fund = fundService.getFundByCode(fundCode);
		if(fund == null){
			return result;
		}
		//获取自治金-福利
		List<Welfare> welfareList = welfareService.listByFundId(fund.getId());
		Map<Long, Welfare> welfareMap = welfareList.stream().collect(Collectors.toMap(Welfare::getId, Function.identity()));
		//分类统计自治事件数量
		List<RewardInfo> rewardInfos = rewardService.listByCondition(year, punishment);
		Map<Long, List<RewardInfo>> rewardMap = rewardInfos.stream().collect(Collectors.groupingBy(RewardInfo::getWelfareId));
		for (Long key : rewardMap.keySet()) {
			String name = welfareMap.get(key) == null ? "" : welfareMap.get(key).getWelfareName();
			String value = rewardMap.get(key) == null ? "0" : String.valueOf(rewardMap.get(key).size());
			result.add(MainPageItem.builder()
					.name(name)
					.count(value)
					.build());
		}
		return result;
	}
}
